﻿using System;
using Csla.Xaml;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism;
using System.ComponentModel.Composition;
using BizControl.Presentation.Infrastructure.Constants;
using System.ComponentModel;
using Microsoft.Practices.Prism.Commands;
using System.Collections.Generic;
using BizControl.Library;
using System.Windows;
using BizControl.Library.BusinessObjects.Notes;
using BizControl.Library.Security;
using Csla;
using System.Windows.Input;

namespace BizControl.Presentation.StickyNotesModule.ViewModels
{
  [Export]
  public class StickyNotesViewModel : ViewModel<NoteList>, INavigationAware, IRegionMemberLifetime
  {
    private readonly DelegateCommand _addNoteCommand;

    private IRegionManager _regionManager;

    [ImportingConstructor]
    public StickyNotesViewModel(IRegionManager regionManager)
    {
      _regionManager = regionManager;
      ManageObjectLifetime = false;
      _addNoteCommand = new DelegateCommand(AddNote);
    }

    #region Commands

    public ICommand AddNoteCommand
    {
      get { return this._addNoteCommand; }
    }

    private void RefreshList()
    {
      DoRefresh(GetList);
    }

    private void AddNote()
    {
      ((BizControlPrincipal)ApplicationContext.User).Person.Notes.AddNewNote();
      RefreshList();
    }

    #endregion

    NoteList GetList()
    {
      return ((BizControlPrincipal)ApplicationContext.User).Person.Notes;
    }

    #region INavigationAware Members

    public bool IsNavigationTarget(NavigationContext navigationContext)
    {
      return true;
    }

    public void OnNavigatedFrom(NavigationContext navigationContext)
    {
    }

    public void OnNavigatedTo(NavigationContext navigationContext)
    {
      RefreshList();
    }

    #endregion

    #region IRegionMemberLifetime Members

    public bool KeepAlive
    {
      get { return true; }
    }

    #endregion

    #region IPartImportsSatisfiedNotification Members

    public void OnImportsSatisfied()
    {
    }

    #endregion
  }
}
